<!DOCTYPE html>
<?php 
require 'func.php';
require_once 'config.php';

$selectedAnswer = "";
$currentQuestion = $num = 1;
$question = getQuestion($currentQuestion);
$studentResult = getStudentResult($_SESSION['studentID']);
function oldGetQuestion($currentQuestion) {
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT * FROM question WHERE questionID = $currentQuestion";
	$result = mysqli_query($con, $query);
	return mysqli_fetch_assoc($result);
	$numberOfRows = mysqli_num_rows($result) or die ("Could not execute query");
	if ($numberOfRows > 0) 
{
	$rowContent= array();
	while ($row = mysqli_fetch_array($result)) 
{
	$questionID = $getQuestion[0][0];
	$questionStem = $getQuestion[0][1];
	$option1 = $getQuestion[0][2];
	$option2 = $getQuestion[0][3];
	$option3 = $getQuestion[0][4];
	$option4 = $getQuestion[0][5];
	$correctAnswer = $getQuestion[0][6];
	$resource = $getQuestion[0][7];	
	$type = $getQuestion[0][8];		
}
	return $rowContent;
} 
}
$getQuestion = oldGetQuestion($currentQuestion);
if (isset($_POST['checkAnswer'])) 
{
		if (isset($_POST['questionOptions']) == $correctAnswer);
{
		$score = ($_POST['score'] + 1);
		if (!isset($_SESSION['rightAnswers']))
{
		$_SESSION['rightAnswers'] = array();
}
		$_SESSION['rightAnswers'][] = $selectedAnswer;
		$feedbackStatus = "<span class='green'> correct.</span>";

}
		if (isset($_POST['questionOptions']) != $getQuestion[0][6]);

{
		$score = ($_POST['score']);
		if (!isset($_SESSION['wrongAnswers']))
{
		$_SESSION['wrongAnswers'] = array();		
}
		$_SESSION['wrongAnswers'][] = $selectedAnswer;
		$feedbackStatus = "<span class='red'> incorrect.</span>";
}

$currentQuestion = $_POST['num'];
$num = $_POST['num'] + 1;
$putStudentResponse = putStudentResponse($_SESSION['studentID'], $attempt, 	$currentQuestion, $testID);
}
if (isset($_POST['getFeedback']))
{
	$num = $_POST['num'] + 1;
	$_SESSION['score'] = $_POST['score']; 
	$score = $_SESSION['score'] ;
} 
	 
if (isset($_POST['getNext']))
{ 
$num = $_POST['num'];
$currentQuestion = $num;
}
$question = getQuestion($currentQuestion);
if ($question['type'] == "multiple-choice"){$displayMC = "block";}
if ($question['type'] == "checkbox"){$displayCB = "block"; }
if ($question['type'] == "true-false"){$displayTF = "block"; }

if (isset($_POST['results'])){	
	header('LOCATION: result.php');			
	} 

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="styles/styles.css" type="text/css"/>
<title>mcq</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="scripts/scripts.js"></script>
</head>
<body>
<header>Learner Centred Design Formative Assessment</header>
<nav>
<?php include 'nav.php' ?>
</nav>
<div id="container">
  <progress id='progress' value='<?php echo $question['questionID']; ?>' max='11'>
    <?php echo $question['questionID']; ?>
  </progress>
  <div id="mainContent">
<?php  if (!isset($_POST['checkAnswer'])) //not started yet
{ 
?>
    <form id="question" method="post">
    <h2>Question
      <?php echo $question['questionID']; ?>
    </h2>
    <div id='resource'>
      <?php echo $question['resource'] ?>
    </div>
    <div class="q-stem">
      <p id="itemStem">
        <?php echo $question['stem'] ?>
      </p>
    </div>
    <div id="multiple-choiceQuestion" style="display:<?php echo $displayMC ?>">
      <div>
        <input name="questionOptions" id="radio1" type="radio" value="<?php echo $question['option1'] ?>">
        <label id="label1" for="radio1">
          <?php echo $question['option1'];?>
        </label>
      </div>
      <div>
        <input name="questionOptions" id="radio2" type="radio" value="<?php echo $question['option2'] ?>">
        <label id="option2" for="radio2">
          <?php echo $question['option2'] ?>
        </label>
      </div>
      <div>
        <input name="questionOptions" id="radio3" type="radio" value="<?php echo $question['option3'] ?>">
        <label id="option3" for="radio3">
          <?php echo $question['option3'] ?>
        </label>
      </div>
      <div>
        <input name="questionOptions" id="radio4" type="radio" value="<?php echo $question['option4'] ?>">
        <label id="option4" for="radio4">
          <?php echo $question['option4'] ?>
        </label>
      </div>
    </div>
    <div id="true-falseQuestion" style="display:<?php echo $displayTF ?>">
      <div>
        <input name="questionOptions" id="radio1" type="radio" value="<?php echo $question['option1'] ?>">
        <label id="label1" for="radio1">
          <?php echo $question['option1'];?>
        </label>
      </div>
      <div>
        <input name="questionOptions" id="radio2" type="radio" value="<?php echo $question['option2'] ?>">
        <label id="option2" for="radio2">
          <?php echo $question['option2'] ?>
        </label>
      </div>
    </div>
    <div id="checkboxQuestion" style="display:<?php echo $displayCB ?>">
      <div>
        <input name="questionOptions" id="checkbox1" type="checkbox" value="<?php echo $question['option1'] ?>">
        <label id="label1" for="checkbox1">
          <?php echo $question['option1'];?>
        </label>
      </div>
      <div>
        <input name="questionOptions" id="checkbox2" type="checkbox" value="<?php echo $question['option2'] ?>">
        <label id="option2" for="checkbox2">
          <?php echo $question['option2'] ?>
        </label>
      </div>
      <div>
        <input name="questionOptions" id="checkbox3" type="checkbox" value="<?php echo $question['option3'] ?>">
        <label id="option3" for="checkbox3">
          <?php echo $question['option3'] ?>
        </label>
      </div>
      <div>
        <input name="questionOptions" id="checkbox4" type="checkbox" value="<?php echo $question['option4'] ?>">
        <label id="option4" for="checkbox4">
          <?php echo $question['option4'] ?>
        </label>
      </div>
    </div>
	<input type="hidden" name="num" value="<?php echo ($num) ?>" />
    <input type="hidden" name="score" value="<?php echo $score ?>" />
    <input type="hidden" name="checkAnswer" value="TRUE" />
    <input type="submit" id="checkAnswerBtn" name="checkAnswerBtn" value="Check Answer" class="btn" />
	</form>
<?php 
}
?>

<?php if (isset($_POST['checkAnswer']))
{
$num = $_POST['num'];  
$_SESSION['score'] = $score;
?>
     <form id="feedback" method="post">
      <p>You answered:<?php echo($_POST['questionOptions']);?>
      </p>
      <p>The correct answer was <?php echo $question['correctAnswer'];?>. That was <?php echo $feedbackStatus?></p>
      <input type="hidden" name="num" value="<?php echo ($num +1) ?>" />
      <input type="hidden" name="score" value="<?php echo $_SESSION['score'] ; ?>" />
      <input type="hidden" name="getNext" value="TRUE" />
      <input type="submit" id="submit" name="next" value="Next Question" class="btn" />
<?php 
}
if($currentQuestion == 10)
{ 
	$last = true; 
	$_SESSION['finished'] = 'yes';
?>
     <input type="hidden" name="num" value="<?php echo ($num +1) ?>" />
     <input type="hidden" name="score" value="<?php echo $_SESSION['score'] ; ?>" />
     <input type="submit" id="results" name="results" value="Get Results" class="btn" />
	</form>
<?php } ?>
    <p class='smiling'>Incorrect Answers:
        <?php echo print_r(array_values($_SESSION['wrongAnswers']))?>
      </p>
      <p class='smiling'>Correct Answers:
        <?php echo print_r(array_values($_SESSION['rightAnswers']))?>
      </p>
      <p class='smiling'>Selected Answers:
        <?php echo $selectedAnswer ?>
      </p>
      <p class='smiling'>Score:
        <?php echo $score ?>
      </p>
      <p class='smiling'>Num:
        <?php echo $num ?>
      </p>
      <p class='smiling'>Last:
        <?php echo $last ?>
        |
        <?php echo ($_SESSION['finished'])?>
      </p>
      <p class='smiling'>Session score:
        <?php echo ($_SESSION['score'])?>
      </p>
      <p class='smiling'>Student ID:
        <?php echo ($_SESSION['studentID'])?>
      </p>
           <p class='smiling'>Current Question:
        <?php echo ($currentQuestion)?>
      </p>
</div>
</div>
</body>
</html>